package lionel.MyController;

import lionel.input.AxisEvent;
import lionel.input.AxisEventListener;
import lionel.input.ButtonEvent;
import lionel.input.ButtonEventListener;
import lionel.input.JInputComponent;

public class MyControllerJInputImpl extends MyController
			implements ButtonEventListener, AxisEventListener
{
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MyControllerJInputImpl.class.getName());
	
	private boolean is2Pressed = false;
	private boolean ignore2release = false;
	private boolean is3Pressed = false;
	private boolean ignore3release = false;
	
	private int mOneCount=0, mTwoCount=0;
	JInputComponent.POV mLastPOV;
	
	public MyControllerJInputImpl(String id) {
		super(id);
		mLastPOV = JInputComponent.POV.CENTER ;
	}
	
	public synchronized void buttonPressed(ButtonEvent evt) {
		if (m_conductor == null) return;
		log.debug("Button pressed: "+evt.getID() );
			
		if (!is2Pressed) is2Pressed = (evt.getID() == JInputComponent.ID.BUTTON__2);
		if (!is3Pressed) is3Pressed = (evt.getID() == JInputComponent.ID.BUTTON__3);
		if (is2Pressed && is3Pressed) // E-stop
		{
			m_conductor.locEStop();
			ignore2release = true; ignore3release = true;
			is2Pressed = false; is3Pressed = false;
			mOneCount = 0; mTwoCount = 0;
		}
		if (evt.getID() == JInputComponent.ID.BUTTON__0) 	// Speed -- loop start
			{ m_conductor.locAcc(true); mOneCount = 0; mTwoCount = 0; }
		if (evt.getID() == JInputComponent.ID.BUTTON__1) 	// Speed ++ loop start
			{ m_conductor.locAcc(false); mOneCount = 0; mTwoCount = 0; }
	}

	public synchronized void buttonReleased(ButtonEvent evt) {
		log.debug("Button released: "+evt.getID() );
		if (m_browser == null) return;
		if (evt.getID() == JInputComponent.ID.BUTTON__8) 	// Select item
		{
			m_browser.select();
			return; 		
		}	
		if (m_conductor == null) return;
		m_conductor.locAccStop();
		if (is2Pressed) 
			is2Pressed = !(evt.getID() == JInputComponent.ID.BUTTON__2);
		if (is3Pressed) 
			is3Pressed = !(evt.getID() == JInputComponent.ID.BUTTON__3);
		if ((!ignore2release) && (evt.getID() == JInputComponent.ID.BUTTON__2))  // slow - idle
		{
			if (mTwoCount == 0)
				{ m_conductor.locSlow(); mTwoCount=1; }
			else if (mTwoCount == 1)
				{ m_conductor.locStop(); mTwoCount=0; }
			mOneCount = 0;
		}
		else
			if ((ignore2release) && (evt.getID() == JInputComponent.ID.BUTTON__2))
				ignore2release = false;
			
		if ((!ignore3release) && (evt.getID() == JInputComponent.ID.BUTTON__3)) 	// cruise - max
		{
			if (mOneCount == 0)
				{ m_conductor.locCruise(); mOneCount=1; }
			else if (mOneCount == 1)
				{ m_conductor.locMax(); mOneCount=0; }
			mTwoCount=0;
		}
		else 
			if ((ignore3release) && (evt.getID() == JInputComponent.ID.BUTTON__3))
				ignore3release = false;

		if (evt.getID() == JInputComponent.ID.BUTTON__4)    // Lights
		{ m_conductor.locSwitchLight(); mOneCount = 0; mTwoCount = 0; }
		if (evt.getID() == JInputComponent.ID.BUTTON__5)	// Direction
		{ m_conductor.locSwitchDir(); mOneCount = 0; mTwoCount = 0; }
		if (evt.getID() == JInputComponent.ID.BUTTON__6)    // TBD
		{ m_conductor.locSwitchF1(); mOneCount = 0; mTwoCount = 0; }
		if (evt.getID() == JInputComponent.ID.BUTTON__7)
		{ m_conductor.locSwitchF2(); mOneCount = 0; mTwoCount = 0; }      
	}
	
	public synchronized void axisMoved(AxisEvent evt) {
		if (m_browser == null) return;
		if ( evt.getID() != JInputComponent.ID.AXIS_POV )
			return;
		if  ((mLastPOV != JInputComponent.POV.CENTER ) && (evt.getPOVValue() == JInputComponent.POV.CENTER ))
		{
			if ( mLastPOV == JInputComponent.POV.RIGHT )
				m_browser.lst1Next();  // Jump to next locomotive
			if ( mLastPOV == JInputComponent.POV.LEFT ) 	
				m_browser.lst1Prev();
			if ( mLastPOV == JInputComponent.POV.UP )	// previous running roster
				m_browser.lst2Next();
			if ( mLastPOV == JInputComponent.POV.DOWN ) 	// next running roster
				m_browser.lst2Prev();

		}
		mLastPOV = evt.getPOVValue();
		return ;
	}
}
